package com.dldata.devtools30.utils;

import com.dldata.devtools30.businessmodel.SessionUserInfoEntity;
import com.dldata.devtools30.cfg.ApplicationHome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * Created by 呵呵 on 2017-10-17.
 */
public class RightUtil {

    private final static Logger logger = LoggerFactory.getLogger(RightUtil.class);

    /**
     *
     * @param request
     * @return
     * @throws IOException
     */
    public static SessionUserInfoEntity getCurrentUser(HttpServletRequest request){

        HttpSession session = request.getSession();
    
        if(!StringUtil.isEmptyOrLength0(request.getParameter(Public.SESSION_USER_KEY))){
            session.setAttribute(Public.SESSION_USER_KEY,new SessionUserInfoEntity(request.getParameter(Public.SESSION_USER_KEY).trim()));
            return new SessionUserInfoEntity(request.getParameter(Public.SESSION_USER_KEY).trim());
        }
        
        SessionUserInfoEntity sessionUserInfoEntity = (SessionUserInfoEntity)session.getAttribute(Public.SESSION_USER_KEY);
        if(sessionUserInfoEntity!=null){
                return sessionUserInfoEntity;
        }

        String  devuser=request.getHeader(Public.SESSION_USER_KEY);
        devuser=devuser==null?request.getParameter(Public.SESSION_USER_KEY):devuser;
        if(!StringUtil.isEmptyOrLength0(devuser)){
            session.setAttribute(Public.SESSION_USER_KEY,new SessionUserInfoEntity(devuser));
            return new SessionUserInfoEntity(devuser);
        }

        Cookie[] cookies = request.getCookies();
        cookies=cookies==null?new  Cookie[0]:cookies;
        for (Cookie cookie : cookies) {
            if (Public.SESSION_USER_KEY.equals(cookie.getName())) {
                session.setAttribute(Public.SESSION_USER_KEY,new SessionUserInfoEntity(cookie.getValue()));
                return new SessionUserInfoEntity(cookie.getValue());
            }
        }

        sessionUserInfoEntity = new SessionUserInfoEntity("");
        return sessionUserInfoEntity;
    }

    /**
     * 校验是否开发环境
     * @param request
     * @return
     */
    public static boolean isDevMode(HttpServletRequest request){
//        logger.info("request:"+request);
//        logger.info("request.getServletContext():"+request.getServletContext());
        ServletContext servletContext = request.getServletContext();
        return isDevMode(servletContext);
    }

    public static boolean isDevMode(ServletContext servletContext) {
        String currentPath = servletContext.getRealPath("/");
//        logger.info("currentPath:"+currentPath);
//        logger.info("System.getProperty(DLDATADEV):"+System.getProperty("DLDATADEV"));
        if(System.getProperty("DLDATADEV")!=null&&System.getProperty("DLDATADEV").equalsIgnoreCase("true")){
            return true;
        }
//        if(System.getenv("DLDATADEV")!=null&&System.getProperty("DLDATADEV").equalsIgnoreCase("true")){
//            return false;
//        }
        if(currentPath==null||!new File(currentPath).exists()){
            return false;
        }
        File rootFileParentFile =  new File(currentPath).getParentFile();
        if(rootFileParentFile.getName().equalsIgnoreCase("target")
                &&(new File(rootFileParentFile.getParentFile(),".idea").exists())
                ||new File(rootFileParentFile.getParentFile(),".ipr").exists()){
            return true;
        }
        rootFileParentFile =  new File(ApplicationHome.getApplicationHome());
        if(new File(rootFileParentFile,"target").exists()
                &&new File(rootFileParentFile,"target").isDirectory()
                &&(new File(rootFileParentFile,".idea").exists())
                ||new File(rootFileParentFile,".ipr").exists()){
            return true;
        }
        return false;
    }


    /**
     * 0. 静态资源
     * 1、例外路径
     * 2、开发模式，开发路径登录校验
     * 3、非开发模式,开发路径,只要登录就可以访问,登录用户devuser只可以查看
     * @param request
     * @return
     */
    public static boolean checkDevRight(HttpServletRequest request) {
        SessionUserInfoEntity sessionUserInfoEntity = RightUtil.getCurrentUser(request);
        String servletPath = request.getServletPath();

        //静态资源
        if(servletPath.startsWith("/static/")||servletPath.startsWith("/CUSTOMFORM/")||servletPath.startsWith("/webservice/")){
            return true;
        }

        return true;
    }
}
